﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using VHKMLib.Common;
using System.Configuration;
using System.IO;

namespace VHWebStudy
{
    /// <summary>
    /// Archive 的摘要说明
    /// </summary>
    public class Archive : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            string opt=context.Request["opt"];
            switch (opt)
            {
                case "get-arc-pageList": GetArcPageList(context);
                    break;
            }
        }

        private void GetArcPageList(HttpContext context)
        {
            List<string> filePathList = new List<string>();
            StringBuilder sb = new StringBuilder();
            string DataPath = ConfigurationManager.AppSettings["DataPath"].ToString();
            string FileViewPath = ConfigurationManager.AppSettings["FileViewPath"].ToString();
            foreach (string f in Directory.GetFiles(DataPath))
            {               
                string filename = Path.Combine(FileViewPath, Path.GetFileNameWithoutExtension(f)+".jpg");
                filePathList.Add(filename);
            }
            Comp c = new Comp();
            filePathList.Sort(c);

            foreach (string f in filePathList)
            {
                sb.Append("<li arcPath='" + f + "' currentpage='"+Path.GetFileNameWithoutExtension(f)+"'>" + Path.GetFileName(f) + "</li>");
            }
            context.Response.Write(sb.ToString());
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }

    //add by Fly Cheng 2017.7.17
    /// <summary>
    /// 根据文件名(文件名为数字)排序
    /// </summary>
    public class Comp : Comparer<string>
    {
        public override int Compare(string x, string y)
        {
            string fileName1 = Path.GetFileNameWithoutExtension(x);
            string fileName2 = Path.GetFileNameWithoutExtension(y);
            return int.Parse(fileName1) - int.Parse(fileName2);
        }
    }
}